﻿using System;
using System.Web;
using System.IO;

namespace Ue.Utility
{
    public class ApplicationHelper
    {
        private HttpApplication application;

        public ApplicationHelper()
            : this(HttpContext.Current.ApplicationInstance) { }

        public ApplicationHelper(HttpApplication application)
        {
            if (application == null)
            {
                throw new ArgumentNullException("application");
            }

            this.application = application;
        }

        /// <summary>
        /// 通过修改web.config的最后修改时间,来重启应用程序
        /// </summary>
        public void RestartApplication()
        {
            string ConfigPath = GetPhysicalApplicationPath() + "web.config";
            File.SetLastWriteTimeUtc(ConfigPath, DateTime.UtcNow);
        }

        /// <summary>
        /// 取得虚拟应用程序的路径
        /// </summary>
        /// <returns></returns>
        public string GetApplicationPath()
        {
            string path = application.Context.Request.ApplicationPath;

            return path == "/" ? string.Empty : path;
        }

        /// <summary>
        /// 取得应用程序根目录的物理路径
        /// </summary>
        /// <returns></returns>
        public string GetPhysicalApplicationPath()
        {
            return application.Context.Request.PhysicalApplicationPath;
        }

        #region Static Methods

        public static string GetApplicationDirectoryPath()
        {
            return AppDomain.CurrentDomain.BaseDirectory;
        }

        #endregion
    }
}
